//package org.jeecg.modules.conss;
//
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.*;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//import javax.annotation.PostConstruct;
//import java.util.Set;
//import java.util.concurrent.*;
//
//@Component
//public class MyWebSocketHandler extends TextWebSocketHandler {
//
//    private static final Set<WebSocketSession> sessions = ConcurrentHashMap.newKeySet();
//    private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
//
//    @PostConstruct
//    public void init() {
//        System.out.println("✅ WebSocket 定时任务启动中...");
//        scheduler.scheduleAtFixedRate(() -> {
//            for (WebSocketSession session : sessions) {
//                if (session.isOpen()) {
//                    try {
//                        session.sendMessage(new TextMessage("服务器定时推送: " + System.currentTimeMillis()));
//                        System.out.println("推送给客户端: " + session.getId());
//                    } catch (Exception e) {
//                        System.err.println("推送失败: " + e.getMessage());
//                    }
//                }
//            }
//        }, 0, 2, TimeUnit.SECONDS);
//    }
//
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        sessions.add(session);
//        session.sendMessage(new TextMessage("连接成功！"));
//        System.out.println("有新连接：" + session.getId());
//    }
//
//    @Override
//    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//        System.out.println("收到前端消息: " + message.getPayload());
//        session.sendMessage(new TextMessage("服务器已收到: " + message.getPayload()));
//    }
//
//    @Override
//    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//        sessions.remove(session);
//        System.out.println("连接关闭：" + session.getId());
//    }
//}
